package com.situ.company.department.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.situ.company.department.model.DepartmentModel;
import com.situ.company.department.service.IDepartmentService;
import com.situ.company.department.service.impl.DepartmentServiceImpl;
import com.situ.company.util.FmtRequest;

@WebServlet("/department")
public class DepartmentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	    doPost(request, response);
	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    Object res = "";
	    switch (req.getParameter("action")) {
        case "list":
            res = list(req);
            break;
        case "page":
            res = page(req);
            break;
        case "add":
            res = add(req);
            break;
        case "del":
            res = del(req);
            break;
        case "get":
            res = get(req);
            break;
        case "get2":
            get2(req);
            req.getRequestDispatcher("/web/page/department/upd2.jsp").forward(req,resp);
            return;
        case "upd":
            res = upd(req);
            break;
        default:
            break;
        }
	    FmtRequest.write(resp.getWriter(), res);
	}

	private Object page(HttpServletRequest req) {
	    Map<String, String> fields = new HashMap<>();
	    fields.put("name", "name");
	    fields.put("code", "code");
	    DepartmentModel model = FmtRequest.parseModel(req, DepartmentModel.class, fields);
	    String pageIndex = req.getParameter("pageIndex");
	    String pageLimit = req.getParameter("pageLimit");
	    model.setPageIndex(Integer.parseInt(pageIndex));
	    model.setPageLimit(Integer.parseInt(pageLimit));
	    model.setPageOn(true);
	    List<DepartmentModel> list = service.selectList(model);
	    int count = service.selectCount(model);
	    Map<String, Object> map = new HashMap<>();
	    map.put("list", list);
	    map.put("count", count);
        return map;
    }

    private void get2(HttpServletRequest req) {
	    DepartmentModel model = new DepartmentModel(req.getParameter("code"));
	    DepartmentModel mdb = service.selectModel(model);
	    req.setAttribute("mdb", mdb);
    }

    private String upd(HttpServletRequest req) {
	    DepartmentModel model = FmtRequest.parseModel(req, DepartmentModel.class);
        return service.update(model);
    }

    private DepartmentModel get(HttpServletRequest req) {
	    DepartmentModel model = new DepartmentModel(req.getParameter("code"));
        return service.selectModel(model);
    }

    private String del(HttpServletRequest req) {
//	    DepartmentModel model = FmtRequest.parseModel(req, DepartmentModel.class);
	    DepartmentModel model = new DepartmentModel(req.getParameter("code"));
        return service.delete(model);
    }

    private String add(HttpServletRequest req) {
	    DepartmentModel model = FmtRequest.parseModel(req, DepartmentModel.class);
        return service.insert(model);
    }

    private IDepartmentService service = new DepartmentServiceImpl();
	
    private List<DepartmentModel> list(HttpServletRequest req) {
        DepartmentModel model = FmtRequest.parseModel(req, DepartmentModel.class);
        return service.selectList(model);
        
    }


}
